﻿using Autofac;
using Castle.Core.Logging;
using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Text;

namespace PlutoStudio.Dependency
{
    /// <summary>
    /// This class is used to register basic dependency implementations such as <see cref="ITransientDependency"/> and <see cref="ISingletonDependency" and <see cref="IInstancePerLifetimeScope"/>/>.
    /// </summary>
    internal sealed class BasicConventionalRegistrar : IConventionalDependencyRegistrar
    {
        public void RegisterAssembly(IConventionalRegistrationContext context)
        {
            context.Builder.RegisterAssemblyTypes(context.Assembly).Where(t => t.IsAssignableTo<ITransientDependency>() && !t.IsGenericTypeDefinition).AsSelf().AsImplementedInterfaces().AsLifeStyle(LifetimeScopeStyle.Transient).PropertiesAutowired(new PropertySelector());
            context.Builder.RegisterAssemblyTypes(context.Assembly).Where(t => t.IsAssignableTo<ISingletonDependency>() && !t.IsGenericTypeDefinition).AsSelf().AsImplementedInterfaces().AsLifeStyle(LifetimeScopeStyle.Singleton).PropertiesAutowired(new PropertySelector());
            context.Builder.RegisterAssemblyTypes(context.Assembly).Where(t => t.IsAssignableTo<IInstancePerLifetimeScope>() && !t.IsGenericTypeDefinition).AsSelf().AsImplementedInterfaces().AsLifeStyle(LifetimeScopeStyle.InstancePerLifetimeScope).PropertiesAutowired(new PropertySelector());
            context.Builder.RegisterAssemblyTypes(context.Assembly).Where(t => t.IsAssignableTo<IInterceptor>() && !t.IsGenericTypeDefinition).AsSelf().AsImplementedInterfaces().AsLifeStyle(LifetimeScopeStyle.Transient).PropertiesAutowired(new PropertySelector());
        }
    }
}
